package edu.mit.media.fv.fluidspace
{
	import edu.mit.media.fv.components.FVButton;
	import edu.mit.media.fv.events.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	public class FluidDebugger extends Sprite
	{
		private var maxY:int = 0;
		private var timer:Timer;
		private var controller:FluidController;
		public function FluidDebugger(controller:FluidController)
		{
			super();
			this.controller = controller;
			
			initButton("Login", doB0);
			initButton("Text", doB1);
			initButton("Poll", doB2);
			initButton("Voice", doB3);
			initButton("Stress", doB4);
			initButton("Random", doB5);
			initButton("VUser", doB6);
			initButton("VText", doB7);
			initButton("VVoice", doB8);
			initButton("VPoll", doB9);
			initButton("VPollResult", doB10);
			initButton("VGroup", doB11);
			initButton("VPoll2", doB12);
			initButton("VShopping", doB13);
			initButton("Tivo", doB14);
			
		}
		
		private function initButton(text:String, f:Function){
			var b:FVButton = new FVButton(0,maxY,50,20,text);
			b.addEventListener(MouseEvent.CLICK, f);
			this.addChild(b);
			maxY = maxY + 20;
		}
		
		private function doB0(event:MouseEvent):void{
			this.controller.login("Kwan");
		}
		private function doB1(event:MouseEvent):void{
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_SEND_OBJECT;
			data.type = FluidConstants.TEXT;
			data.tags = "Test Text Messaging";
			data.priority = 1;
			data.text = "Hello Kwan.  This is a test message sent from the FluidVoice UI to the FluidVoice engine.";
			data.id = Math.floor(Math.random()*23874);
			data.reply = Math.floor(Math.random()*23874);
			var fvne:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(fvne);
		}
		private function doB2(event:MouseEvent):void{
			
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_SEND_OBJECT;
			data.type = FluidConstants.POLL;
			data.tags = "Test Poll Mesaging";
			data.question = "What sort of fruits do you like?";
			data.priority = 1;
            data.poll_type = 0;
			data.duration = 1;
			data.choices = ["Apple", "Banana", "Orange", "Kiwi"];
			data.id = -1;
			var e:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(e);
		}
		private function doB3(event:MouseEvent):void{
			var timer:Timer = new Timer(1000,20);
			timer.addEventListener(TimerEvent.TIMER, doB3Timer);
			timer.start();
		}
		private function doB3Timer(event:TimerEvent):void{
			trace("B3Timer Running at count "+Timer(event.target).currentCount+" of "+Timer(event.target).repeatCount);
			if(Timer(event.target).currentCount == 5){
				trace("Recording Started.");
				//this.controller.requestStartRecord(15);
			}else if(Timer(event.target).currentCount == 13){
				trace("Stopped Recording.");
				//this.controller.requestStopRecord(15);
			}else if(Timer(event.target).currentCount == 15){
				trace("Sending Voice Mail");
				var data:Object = new Object();
				data.command = FluidConstants.REQUEST_SEND_OBJECT;
				data = new Object();
				data.id = -1;
				data.reply = -1;
				data.command = FluidConstants.REQUEST_SEND_OBJECT;
				data.type = FluidConstants.VOICE;
				data.tags = "Test Voice Mail";
				data.priority = 1;
				var fvne:FVNetworkEvent = new FVNetworkEvent(data);
				FluidEventHandler.dispatchEvent(fvne);
			}
		}
		private function doB4(event:MouseEvent):void{
			var timer:Timer = new Timer(500,10);
			timer.addEventListener(TimerEvent.TIMER, doB4Timer);
			timer.start();
		}
		private function doB4Timer(event:TimerEvent):void{
			trace("B4Timer Running at count "+Timer(event.target).currentCount+" of "+Timer(event.target).repeatCount);
			var data:Object = new Object();
			data.command = FluidConstants.REQUEST_SEND_OBJECT;
			data.type = FluidConstants.TEXT;
			data.tags = "Test";
			data.priority = 1;
			data.text = "Stress Testing";
			data.id = Math.floor(Math.random()*23874);
			var fvne:FVNetworkEvent = new FVNetworkEvent(data);
			FluidEventHandler.dispatchEvent(fvne);
		}
		private function doB5(event:MouseEvent):void{
			var e:UpdateEvent = new UpdateEvent(UpdateEvent.ADD_USER, 1);
			e.label = "Jake";
			e.status = 0;
			FluidEventHandler.dispatchEvent(e);
			
			e = new UpdateEvent(UpdateEvent.ADD_USER, 2);
			e.label = "Matt";
			e.status = 1;
			FluidEventHandler.dispatchEvent(e);
			
			e = new UpdateEvent(UpdateEvent.ADD_USER, 22);
			e.label = "David";
			e.status = 1;
			FluidEventHandler.dispatchEvent(e);
			
			e = new UpdateEvent(UpdateEvent.ADD_USER, 23);
			e.label = "Charles";
			e.status = 3;
			FluidEventHandler.dispatchEvent(e);
			
			
			e = new UpdateEvent(UpdateEvent.ADD_USER, 24);
			e.label = "Andy";
			e.status = 2;
			FluidEventHandler.dispatchEvent(e);
			
			e = new UpdateEvent(UpdateEvent.ADD_SERVICE, 3);
			e.label = "Service1";
			e.status = 1;
			FluidEventHandler.dispatchEvent(e);
			
			e = new UpdateEvent(UpdateEvent.ADD_GROUP, 4);
			e.label = "Viral Comm";
			e.status = 1;
			FluidEventHandler.dispatchEvent(e);
			
			e = new UpdateEvent(UpdateEvent.ADD_TEXT, 5);
			e.label = "Urgent";
			e.authorID = 1;
			e.time = new Date().time - 1*60*60*1000;
			FluidEventHandler.dispatchEvent(e);
			
			e = new UpdateEvent(UpdateEvent.ADD_VOICE, 6);
			e.label = "Seminar";
			e.authorID = 1;
			e.time = new Date().time - 67*60*60*1000*24;
			FluidEventHandler.dispatchEvent(e);
			
			e = new UpdateEvent(UpdateEvent.ADD_POLL, 6);
			e.label = "Lunch";
			e.authorID = 1;
			e.time = new Date().time - 15*60*60*1000*24;
			FluidEventHandler.dispatchEvent(e);
			
			e = new UpdateEvent(UpdateEvent.ADD_SHOPPING, 6234);
			e.label = "StarMarket";
			e.authorID = 1;
			e.time = new Date().time - 19*60*60*1000*24;
			FluidEventHandler.dispatchEvent(e);
			
		}
		private function doB6(event:MouseEvent):void{
			//view user
			
			var e:DisplayEvent = new DisplayEvent(DisplayEvent.OPEN_USER, 225);
			e.name = "Random User";
			e.volume = 1.0;
			e.balance = 0;
			if (0 == 0){
				e.quality = false;
			}else{
				e.quality = true;
			}
			
			FluidEventHandler.dispatchEvent(e);
		}
		private function doB7(event:MouseEvent):void{
			//view text
			var e:DisplayEvent = new DisplayEvent(DisplayEvent.OPEN_TEXT, 225);
			e.subject = "Tags are here";
			e.text = "Text here";
			e.sourceName = "Andy";
			
			FluidEventHandler.dispatchEvent(e);
		}
		private function doB8(event:MouseEvent):void{
			//view voice
			var e:DisplayEvent = new DisplayEvent(DisplayEvent.OPEN_VOICE, 425);
			e.subject = "Tags are here";
			e.sourceName = "Kwan";
			FluidEventHandler.dispatchEvent(e);
		}
		private function doB9(e:MouseEvent):void{
			//view poll
			var event:DisplayEvent = new DisplayEvent(DisplayEvent.OPEN_POLL, 2738);
			event.subject = "Tags";
			event.question = "Question";
			event.category = 0;
			event.choices = ["A","B","C"];
			event.sourceName = "blah";
			FluidEventHandler.dispatchEvent(event);
		}
		private function doB10(e:MouseEvent):void{
			//view poll reuslt
			var random:Number = Math.random();
			if(random<0.5){
				var event:DisplayEvent = new DisplayEvent(DisplayEvent.OPEN_POLL_RESULT, 765767);
				event.subject = "Poll Results Tags";
				event.question = "What fruits do you like?";
				event.category = 1;
				event.sourceName = "John";
				var results:Dictionary = new Dictionary();
				for(var i:int = 0; i<5; i++){
					var candidate:String = "Fruit Type "+i;
					results[candidate] = i*2;
				}
				event.results = results;
				FluidEventHandler.dispatchEvent(event);
			}else{
				event = new DisplayEvent(DisplayEvent.OPEN_POLL_RESULT, 765767);
				event.subject = "Poll Results Tags";
				event.question = "What fruits do you like?";
				event.category = 1;
				event.sourceName = "John";
				results = new Dictionary();
				for(i = 0; i<10; i++){
					candidate = "Fruit Type "+i;
					results[candidate] = i*3*Math.random();
				}
				event.results = results;
				FluidEventHandler.dispatchEvent(event);
			}
		}
		private function doB11(event:MouseEvent):void{
			//view group
			var e:DisplayEvent = new DisplayEvent(DisplayEvent.OPEN_GROUP, 225);
			e.name = "Random Group";
			e.volume = 1.0;
			e.balance = 0;
			if (0 == 0){
				e.quality = false;
			}else{
				e.quality = true;
			}
			
			FluidEventHandler.dispatchEvent(e);
			
		}
		private function doB12(e:MouseEvent):void{
			//view poll
			var event:DisplayEvent = new DisplayEvent(DisplayEvent.OPEN_POLL, 2738);
			event.subject = "Tags";
			event.question = "Question";
			event.category = 1;
			event.choices = ["A","B","C"];
			event.sourceName = "blah";
			FluidEventHandler.dispatchEvent(event);
		}
		private function doB13(event:MouseEvent):void{
			//view group
			var e:DisplayEvent = new DisplayEvent(DisplayEvent.OPEN_SHOPPING, 225);
			e.subject = "Third East Party";
			e.store = "StarMarket";
			e.choices = ["orange","beer","balloons"];
			e.sourceName = "Aidan";
			e.duration = "1 hour";
			FluidEventHandler.dispatchEvent(e);
		}
		
		private function doB14(event:MouseEvent):void {
		}
		
		public static function getTimelineMessageCount(date:Date, gran:int):TimelineEvent{
			var debugEvent:TimelineEvent = new TimelineEvent(TimelineEvent.UPDATE_TIMELINE, date);
			debugEvent.messages = new Array();
			var o1:Object = new Object();
			o1.msg_type = FluidConstants.TEXT;
			o1.count = 100;
			debugEvent.messages.push(o1);
			
			o1 = new Object();
			o1.msg_type = FluidConstants.VOICE;
			o1.count = 69;
			debugEvent.messages.push(o1);
			
			o1 = new Object();
			o1.msg_type = FluidConstants.POLL;
			o1.count = 31;
			debugEvent.messages.push(o1);
			
			o1 = new Object();
			o1.msg_type = FluidConstants.PIX;
			o1.count = 78;
			debugEvent.messages.push(o1); 
			
			o1 = new Object();
			o1.msg_type = FluidConstants.SHOPPING;
			o1.count = 12;
			debugEvent.messages.push(o1);
			
			return debugEvent;
		}
		
		public static function getTimelineMessages(date:Date, gran:int):TimelineEvent{
			var debugEvent:TimelineEvent = new TimelineEvent(TimelineEvent.UPDATE_TIMELINE,date, gran);
			debugEvent.start = date;
			debugEvent.end = date;
			debugEvent.messages = new Array();
			var o1:Object = new Object();
			o1.id = 10;
			o1.src_id = 20;
			o1.reply_id = 9;
			o1.src_name = "John Adams";
			o1.tags = "Birthday Party";
			o1.text = getRandString();
			debugEvent.messages.push(o1);
			
			o1 = new Object();
			o1.id = 11;
			o1.src_id = 21;
			o1.reply_id = 10;
			o1.src_name = "Jane Smith";
			o1.tags = "RE: Birthday Party";
			o1.text = getRandString();
			debugEvent.messages.push(o1);
			
			o1 = new Object();
			o1.id = 12;
			o1.src_id = 20;
			o1.reply_id = 10;
			o1.src_name = "John Adams";
			o1.tags = "RE: Birthday Party";
			o1.text = getRandString();
			debugEvent.messages.push(o1);
			
			o1 = new Object();
			o1.id = 13;
			o1.src_id = 24;
			o1.reply_id = 29;
			o1.src_name = "Betty Jay";
			o1.tags = "Free Software";
			o1.text = getRandString();
			debugEvent.messages.push(o1); 
			
			o1 = new Object();
			o1.id = 14;
			o1.src_id = 22;
			o1.reply_id = 11;
			o1.src_name = "Roger Hugh";
			o1.tags = "Free apples in main lounge";
			o1.text = getRandString();
			debugEvent.messages.push(o1); 
			
			o1 = new Object();
			o1.id = 15;
			o1.src_id = 20;
			o1.reply_id = 11;
			o1.src_name = "John Adams";
			o1.tags = "Who is Going?";
			o1.text = getRandString();
			debugEvent.messages.push(o1);
			
			return debugEvent;
		}
		
		private static var randCounter:int = 0;
		private static var randStringArr:Array = ["As"," always,"," my"," passes"," at ","omniscience"," are"," absurd,"," but ","you, ","of ","all ","people, ","should"," be"," polite"," to ","the"," part ","of"," me ","that ","comes ","out ","merely"," clever"];
		private static function getRandString():String{
			var randomString:String = "";
			if(randCounter < randStringArr.length){
				randomString= randStringArr[randCounter];
			}else{
				randCounter = 0;
				randomString= randStringArr[randCounter];
			}
			randCounter = randCounter+1;
			return randomString;
		}
		private static function getRandNumber():int{
			return int(Math.round(Math.random()*1000))*100;
		}

	}
}